package cn.bxshey.configModule.ws;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import javax.annotation.Resource;

/**
 * @作者：不想说话而已
 * @Date：2024/3/8 21:50
 *
 * WebSocket 配置
 */

@Configuration
@EnableWebSocket
public class WsConfig implements WebSocketConfigurer {
    //自定义处理器
    @Resource
    WsHandler wsHandler;

    //自定义拦截器
    @Resource
    WsInterceptor wsInterceptor;

    @Bean
    public ServerEndpointExporter serveEndpointExporter() {
        return new ServerEndpointExporter();
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        //配置处理器，拦截器，容许所有的ip访问
        registry.addHandler(wsHandler, "/ws").addInterceptors(wsInterceptor).setAllowedOrigins("*");
    }
}
